package com.applications.codemash.groupers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.applications.codemash.core.GrouperBase;
import com.applications.codemash.core.Session;
import com.applications.codemash.utility.StringDateComparator;

public class SessionListGroupByDay extends GrouperBase
{
	@Override
	public Map<String, List<Session>> Group(List<Session> sessionList)
	{
		Map<String, List<Session>> returnMap = new HashMap<String, List<Session>>();
		for (int i=0; i<sessionList.size(); i++)
		{
			String dayString = sessionList.get(i).getStartDisplay();
			if (!returnMap.containsKey(dayString))
			{
				returnMap.put(dayString, new ArrayList<Session>());
			}
			
			returnMap.get(dayString).add(sessionList.get(i));
		}
		
		returnMap = Sort(returnMap);
		return returnMap;
	}

	@Override
	protected Map<String, List<Session>> Sort(Map<String, List<Session>> listing)
	{
		Map<String, List<Session>> returnMap = new TreeMap<String, List<Session>>(new StringDateComparator());
		for (String key : listing.keySet())
		{
			List<Session> list = listing.get(key);
			Collections.sort(list);
			returnMap.put(key, list);
		}
		
		return returnMap;
	}
}
